home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / ms_dos / keyin / keyin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  1.7 KB  |  96 lines

  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define u_int unsigned int
  7. #define ON (-1)
  8. #define OFF (0)
  9. #define DEBUG
  10.  
  11. char Myname[9];  /* Target command name. It has to be inticalized in main(). */
  12.  
  13. void print_usage(void)
  14. {
  15.     puts("Check key input in console. ver 1.0 93/2/26");
  16.     printf("usage: %s [-hv]\n", Myname);
  17.     puts("      -h Print this help message");
  18.     puts("      -v Print input key");
  19. }
  20.  
  21. char *short_name(char *name, char *path)
  22. {
  23.     char *p, *pp;
  24.  
  25.     pp = name;
  26.     if ((p = strrchr(path, '/')) == NULL) {
  27.         if (path[1] == ':')
  28.             p = &path[2];
  29.         else
  30.             p = path;
  31.     } else
  32.         p++;
  33.  
  34.     while (*p != '.' && *p != '\0') {
  35.         *name = *p;
  36.         p++;
  37.         name++;
  38.     }
  39.     *name = '\0';
  40.     return pp;
  41. }
  42.  
  43. /*  version non-suported two bytes code  */
  44. char *replace_str(char *s, int c1, int c2)
  45. {
  46.     char    *p;
  47.  
  48.     for (p=s; *p != '\0'; p++)
  49.         if (*p == (char) c1)
  50.             *p = (char) c2;
  51.     return s;
  52. }
  53.  
  54. int main(int argc, char **argv)
  55. {
  56.     int Flag_v = OFF;
  57.     int c;
  58.     
  59.     replace_str(argv[0], '\\', '/');
  60.     short_name(argv[0], Myname);
  61.     
  62.     if (argc == 2) {
  63.         if (argv[1][0] == '-' || argv[1][0] == '/') {
  64.             if (argv[1][1] == 'h') {
  65.                 print_usage();
  66.                 exit(EXIT_SUCCESS);
  67.             } else if (argv[1][1] == 'v') {
  68.                 Flag_v = ON;
  69.             }
  70.         } else {
  71.             fprintf(stderr, "Option Error!\n");
  72.             print_usage();
  73.             exit(EXIT_FAILURE);
  74.         }
  75.     } else if (argc > 2) {
  76.         fprintf(stderr, "Too many options!\n");
  77.         print_usage();
  78.         exit(EXIT_FAILURE);
  79.     }
  80.     
  81.     if (kbhit()) {
  82.         if (Flag_v) {
  83.             c = getche();
  84.         } else {
  85.             c = getch();
  86.         }
  87. #ifdef DEBUG
  88.         fprintf(stderr, "Return code is %x.\n", (u_int) c);
  89. #endif
  90.         return c;
  91.     } else {
  92.         return 0;
  93.     }
  94. }
  95.  
  96.